package com.paas.form.core.model;

import com.gencode.util.CodeBuildNew;
import com.paas.common.util.BeanUtil;
import com.paas.common.util.StringUtil;
import com.paas.form.core.element.field.Config;
import com.paas.form.core.element.field.FieLdsModel;
import com.paas.form.core.element.field.Option;
import com.paas.form.core.element.field.Slot;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@EqualsAndHashCode(callSuper = true)
@Data
public class DivComponent extends ElementUI {

    private String prefixName;

    private FieLdsModel field;

    private int indentNum;

    private Map<String, Object> params;



    public DivComponent(String prefixName, FieLdsModel field, int indentNum) {
        this.prefixName = prefixName;
        this.field = field;
        this.params = new HashMap<>();
        this.params.put("field",this.field);

        //
        this.indentNum = indentNum;
    }

    public static final String DIV_COMPONENT_HTML = "#set($mastModel=\"${field.vModel}\")\n" +
            "#set($html = $field)\n" +
            "#set($vModel = \"${html.vModel}\")\n" +
            "#set($config = $html.config)\n" +
            "#set($mastKey = \"${config.pfKey}\")\n" +
            "#set($show = $config.noShow)\n" +
            "#set($pcshow = $config.pc)\n" +
            "<${config.tag} #if($vModel)  v-model=\"${formDataModel.formModel}.${mastModel}\" @change=\"changeData('${vModel}',-1)\"  #end\n" +
            "#if($mastKey!='PFText')\n" +
            "#if($html.placeholder) placeholder=\"${html.placeholder}\" #end" +
            "#else\n" +
            "#if($mastKey == 'relationFormAttr' || $mastKey == 'popupAttr')\n" +
            "#if($config.defaultValue) defaultValue=\"${config.defaultValue}\"#end\n" +
            "#else\n" +
            "#if($config.defaultValue) value=\"${config.defaultValue}\"#end\n" +
            "#end\n" +
            "#end\n" +
            "#if($mastKey== 'cascader')\n" +
            "#if($html.showAllLevels)\n" +
            "show-all-levels\n" +
            "#else\n" +
            ":show-all-levels=\"false\"\n" +
            "#end\n" +
            "#end\n" +
            "#if($mastKey =='popupSelect' || $mastKey =='popupTableSelect')\n" +
            ":rowIndex=\"null\" :formData=\"${context.formModel}\" :templateJson='${html.templateJson}'\n" +
            "#end\n" +
            "#if($mastKey== 'uploadFz' || $mastKey== 'uploadImg')\n" +
            "#if(${html.fileSize}) :fileSize=\"${html.fileSize}\" #else :fileSize=\"null\" #end  #end\n" +
            "#if($html.maxlength) :maxlength=\"${html.maxlength}\" #end\n" +
            "#if($html.readonly == true ) readonly #end\n" +
            "#if($html.disabled == true ):disabled=\"${html.disabled}\"#end\n" +
            "#if($html.clearable == true ) clearable #end\n" +
            "#if($html.prefixicon) prefix-icon='${html.prefixicon}' #end\n" +
            "#if($html.suffixicon) suffix-icon='${html.suffixicon}' #end\n" +
            "#if($html.style) :style='${html.style}'#end" +
            "#if($html.showWordLimit == true ) ${html.showWordLimit} #end\n" +
            "#if($html.size) size=\"${html.size}\" #end\n" +
            "#if($html.min) :min=\"${html.min}\" #end\n" +
            "#if($html.max) :max=\"${html.max}\" #end\n" +
            "#if($html.type) type=\"${html.type}\" #end\n" +
            "#if($html.autosize) :autosize='${html.autosize}' #end\n" +
            "#if($html.step) :step=\"${html.step}\" #end\n" +
            "#if($html.precision) :precision=\"${html.precision}\" #end\n" +
            "#if($html.stepstrictly==true) stepstrictly #end\n" +
            "#if($html.textStyle) :textStyle='${html.textStyle}' #end\n" +
            "#if($html.lineHeight) :lineHeight=\"${html.lineHeight}\" #end\n" +
            "#if($html.fontSize) :fontSize=\"${html.fontSize}\" #end\n" +
            "#if($html.controlsposition) controls-position='${html.controlsposition}' #end\n" +
            "#if($html.showChinese) :showChinese=\"${html.showChinese}\" #end\n" +
            "#if($html.showPassword) show-password #end\n" +
            "#if($html.filterable==true) filterable #end\n" +
            "#if($html.multiple) :multiple=\"${html.multiple}\" #end\n" +
            "#if($html.separator) separator=\"${html.separator}\" #end\n" +
            "#if($html.isrange==true) is-range #end\n" +
            "#if($html.rangeseparator) range-separator=\"${html.rangeseparator}\" #end\n" +
            "#if($html.startplaceholder) start-placeholder=\"${html.startplaceholder}\" #end\n" +
            "#if($html.endplaceholder) end-placeholder=\"${html.endplaceholder}\" #end\n" +
            "#if($html.format) format=\"${html.format}\" #end\n" +
            "#if($html.colorformat) color-format=\"${html.colorformat}\" #end\n" +
            "#if($html.valueformat) value-format=\"${html.valueformat}\" #end\n" +
            "#if($html.activetext) active-text=\"${html.activetext}\" #end\n" +
            "#if($html.inactivetext) inactive-text=\"${html.inactivetext}\" #end\n" +
            "#if($html.activecolor) active-color=\"${html.activecolor}\" #end\n" +
            "#if($html.inactivecolor) inactive-color=\"${html.inactivecolor}\" #end\n" +
            "#if($html.activevalue) :active-value=\"${html.activevalue}\" #end\n" +
            "#if($html.inactivevalue) :inactive-value=\"${html.inactivevalue}\" #end\n" +
            "#if($html.pickeroptions) :picker-options='${html.pickeroptions}'#end\n" +
            "#if($html.showScore == true ) show-score #end\n" +
            "#if($html.showText == true ) show-text #end\n" +
            "#if($html.allowhalf == true ) allow-half #end\n" +
            "#if($html.showAlpha == true ) show-alpha #end\n" +
            "#if($html.showStops == true ) show-stops #end\n" +
            "#if($html.range == true ) range #end\n" +
            "#if($html.showTip == true ) :showTip=\"${html.showTip}\" #end\n" +
            "#if($html.accept) accept=\"${html.accept}\" #end\n" +
            "#if($html.sizeUnit) sizeUnit=\"${html.sizeUnit}\" #end\n" +
            "#if($html.limit) :limit=\"${html.limit}\" #end\n" +
            "#if($html.buttonText) buttonText=\"${html.buttonText}\" #end\n" +
            "#if($html.contentposition) content-position=\"${html.contentposition}\" #end\n" +
            "#if($!html.level ):level=${html.level} #end\n" +
            "#if($mastKey == 'relationFormAttr' || $mastKey == 'popupAttr')\n" +
            "#if($html.relationField) relationField=\"${html.relationField}\" #end\n" +
            "#if($html.showField) showField=\"${html.showField}\" #end\n" +
            "#if($config.isStorage) isStorage=$config.isStorage #end\n" +
            "#end >" +
//            "    #if($html.selectType) selectType=\"$html.selectType\" #end\n" +
//            "    #if($html.selectType == 'custom')\n " +
//            "    #if($html.ableDepIds) :ableDepIds = '$html.ableDepIds' #end\n" +
//            "    #if($html.ablePosIds) :ablePosIds = '$html.ablePosIds' #end\n" +
//            "    #if($html.ableUserIds) :ableUserIds = '$html.ableUserIds' #end\n" +
//            "    #if($html.ableRoleIds) :ableRoleIds = '$html.ableRoleIds' #end\n" +
//            "    #if($html.ableGroupIds) :ableGroupIds = '$html.ableGroupIds' #end\n" +
//            "    #if($html.ableIds) :ableIds = 'JSON.parse($html.ableIds)' #end\n" +
//            "    #elseif($html.selectType == 'dep' || $html.selectType == 'pos' || $html.selectType == 'role' || $html.selectType == 'group')\n" +
//            "    #if($html.relationField)\n" +
//            "    :ableRelationIds=\" Array.isArray(dataForm.${html.relationField}) ? dataForm.${html.relationField} : [dataForm.${html.relationField}]\"\n" +
//            "    #end\n" +
//            "    #end\n" +
//            "    #if($mastKey == 'relationForm') field=\"${vModel}\" modelId =\"${html.modelId}\"  :columnOptions=\"${vModel}columnOptions\" relationField=\"${html.relationField}\" popupWidth=\"${html.popupWidth}\" #if($html.hasPage) hasPage :pageSize=\"$html.pageSize\" #end  #end\n" +
//            "    #if($mastKey == 'popupSelect' ||$mastKey == 'popupTableSelect')\n" +
//            "    field=\"${vModel}\" interfaceId=\"${html.interfaceId}\" :columnOptions=\"${vModel}columnOptions\" propsValue=\"${html.propsValue}\" relationField=\"${html.relationField}\" popupType=\"${html.popupType}\"\n" +
//            "    #if(${html.popupTitle}) popupTitle=\"${html.popupTitle}\" #end popupWidth=\"${html.popupWidth}\"\n" +
//            "    #if($html.hasPage) hasPage :pageSize=\"$html.pageSize\" #end  #end\n" +
//            "    #if($mastKey=='cascader' || $mastKey=='treeSelect') :options=\"${vModel}Options\" :props=\"${vModel}Props\" #end>\n" +
//            "    #if($mastKey=='checkbox'||$mastKey=='radio'||$mastKey=='select')\n" +
            "    #set($name=\"${config.props.label}\")\n" +
            "    #set($id=\"${config.props.value}\")" +
            "    #if($mastKey=='select')\n" +
            "    \n<el-option v-for=\"(item, index) in ${html.vModel}Options\" :key=\"index\" :label=\"item.${name}\" :value=\"item.${id}\" :disabled=\"item.disabled\" #if($config.border == true )border #end></el-option>\n" +
            "    #end\n" +
//            "    <el-${mastKey}#if($config.optionType==\"button\")-button #end v-for=\"(item, index) in ${html.vModel}Options\" :key=\"index\" :label=\"item.${id}\" :disabled=\"item.disabled\" #if($config.border == true )border #end>\n" +
//            "    {{item.${name}}}\n" +
//            "    </el-${mastKey}#if($config.optionType==\"button\")-button #end>\n" +
//            "    #end\n" +
//            "    #end\n" +

            "    #if($mastKey!='checkbox'&&$mastKey!='radio'&& $mastKey!='select')\n" +
            "    #if($html.schemeSlot.prepend)\n" +
            "    <template schemeSlot=\"prepend\">${html.schemeSlot.prepend}</template>\n" +
            "    #end\n" +
            "    #if($html.schemeSlot.append)\n" +
            "    <template schemeSlot=\"append\">${html.schemeSlot.append}</template>\n" +
            "    #end\n" +
            "    #end\n" +
            "    #end";

    
    @Override
    public void getElHtml(StringBuilder str) {

    }

    @Override
    public void getStartTagElHtml(StringBuilder str) {
        if(BeanUtil.isNotEmpty(this.field.getConfig())&&StringUtil.isNotEmpty(this.field.getConfig().getTag())){
            //
            StringBuilder stringBuilder = new StringBuilder();
            //
            String codeTemplate = String.join("", Collections.nCopies(indentNum, "\t")) + DIV_COMPONENT_HTML;
            stringBuilder.append(codeTemplate);
            //
            //
            String elsl = stringBuilder.substring(0, stringBuilder.lastIndexOf("\n"));

            String CodeString = CodeBuildNew.getCodeString(this.params,elsl);
            //
            String twoCodeString= String.join("", Collections.nCopies(indentNum, "\t")) +CodeString.replaceAll("\n","");
            str.append(twoCodeString);
            str.append("\n");
        }

    }

    @Override
    public void getEndTagElHtml(StringBuilder str) {
        if(BeanUtil.isNotEmpty(this.field.getConfig())&&StringUtil.isNotEmpty(this.field.getConfig().getTag())){
            String codeTemplate = String.join("", Collections.nCopies(indentNum, "\t")) + "</${field.config.tag}>";
            String endCodeString = CodeBuildNew.getCodeString(this.params,codeTemplate);
            str.append(endCodeString);
            str.append("\n");
        }

    }


}